<?php

namespace app\admin\model\cms;

use think\Model;


class Video extends Model
{

    // 表名
    protected $name = 'video';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    protected $createTime = "create_time";
    protected $updateTime = "update_time";

    // 追加属性
    protected $append = [
        'category_name',
        'image_obs',
        'video_path_obs',
    ];

    public function getImageObsAttr($value, $data)
    {
        $value = $data['image'] ? obsurl($data['image']) : '';
        return $value;
    }

    public function getVideoPathObsAttr($value, $data)
    {
        $value = $data['video_path'] ? obsurl($data['video_path']) : '';
        return $value;
    }

    //获取分类名称
    public function getCategoryNameAttr($value, $data)
    {
        $value = $value ? $value : $data['video_category_id'];
        $name  = VideoCategoryModel::where('id', '=', $value)->value('platename');
        return !empty($name) ? $name:'';
    }

    protected function setLiveTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function getCreateTimeAttr($value)
    {
        return (is_numeric($value) && $value > 0) ? date('Y-m-d H:i:s', $value) : "";
    }

    protected function getUpdateTimeAttr($value)
    {
        return (is_numeric($value) && $value > 0) ? date('Y-m-d H:i:s', $value) : "";
    }

    public function user()
    {
        return $this->belongsTo("app\common\model\User", "user_id", "id", [], "LEFT")->field('id,nickname,avatar,bio,email')->setEagerlyType(1);
    }

    public function category()
    {
        return $this->belongsTo('app\admin\model\cms\VideoCategoryModel', 'video_category_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

}
